package logic

import (
	"context"
	"errors"
	"manuscript-service/mysql/models"

	"manuscript-service/manrpc/internal/svc"
	"manuscript-service/manrpc/manrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GaoAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGaoAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GaoAddLogic {
	return &GaoAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GaoAddLogic) GaoAdd(in *manrpc.GaoAddRequest) (*manrpc.GaoAddResponse, error) {
	// todo: add your logic here and delete this line
	begin := models.DB.Begin()

	var fen models.Fen
	err := fen.FenList(models.DB, in.Title)
	if err != nil {
		begin.Rollback()
		return nil, err
	}
	var user models.Gao
	err = user.GaoShow(models.DB, in.Name)
	if err == nil {
		begin.Rollback()
		return nil, errors.New("已存在")
	}
	user = models.Gao{
		Name:  in.Name,
		Like:  int(in.Like),
		Image: in.Image,
		Fid:   int(fen.ID),
	}
	err = user.GaoAdd(models.DB)
	if err != nil {
		begin.Rollback()
		return nil, err
	}
	return &manrpc.GaoAddResponse{
		GoaId: int64(user.ID),
	}, nil
}
